﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.IO;
using System.Configuration;

public partial class Page_SMSTest : Page//BaseAuthenticationPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (file.PostedFile == null)
        {
            ClientScript.RegisterStartupScript(GetType(), "noFile", "alert('请先选择Excel文档！');", true);
            return;
        }
        string Dir = Server.MapPath("~/temp");
        if (!Directory.Exists(Dir))
            Directory.CreateDirectory(Dir);
        string fileName = Dir + "/" + Guid.NewGuid() + ".xls";
        file.PostedFile.SaveAs(fileName);
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";Extended Properties=Excel 8.0;");
        conn.Open();
        OleDbDataAdapter adp = new OleDbDataAdapter("select * from [Sheet1$]",conn);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=SMSStatus.xls");
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        string str = "<table><tr><td>手机号码</td><td>发送状态</td></tr>";
        foreach (DataRow dr in dt.Rows)
        {
            MessagesService service = new MessagesService();
            string status = service.SendMessageByModuleID(ModuleID, SenderName, getReceiver("test", "test", dr["手机号码"].ToString()), txtContent.Text, txtTitle.Text, false, 1, string.Empty, SendTime);
            if (string.IsNullOrEmpty(status)) status = "发送成功";
            str += "<table><tr><td>" + dr["手机号码"].ToString() + "</td><td>"+status+"</td></tr>";
        }
        str += "</table>";
        conn.Close();
        foreach (string _fileName in Directory.GetFiles(Dir))
        {
            File.Delete(_fileName);
        }
        Response.Write(str);
        Response.End();
    }
    public string getReceiver(string userName, string userDisplayName, string Mobile)
    {
        return string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?>
                                    <Receivers>
  			                                   <Receiver>
    				                                    <Name>{0}</Name>
    				                                    <DisplayName>{1}</DisplayName>
    				                                    <NodeType>User</NodeType>
    				                                    <Email></Email>
    				                                    <LCS></LCS>
    				                                    <Mobile>{2}</Mobile>
    				                                    <Fax></Fax>
  			                                    </Receiver>
                                    </Receivers>", userName, userDisplayName, Mobile);
    }
    private string ModuleID
    {
        get
        {
            return string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?><value type=""System.Guid"">{0}</value>", ConfigurationManager.AppSettings["ModuleID"]);
        }
    }
    private string SendTime
    {
        get
        {
            return string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?><value type=""System.DateTime"">{0}</value>", DateTime.Now);
        }
    }
    private string SenderName
    {
        get
        {
            return ConfigurationManager.AppSettings["SenderName"];
        }
    }
}